package autocode

import (
	"time"

	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/autocode"
	autoCodeReq "github.com/flipped-aurora/gin-vue-admin/server/model/autocode/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
)

type UserInfoService struct {
}

// CreateUserInfo 创建UserInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (userInfoService *UserInfoService) CreateUserInfo(userInfo autocode.UserInfo) (err error) {
	err = global.GVA_DB.Create(&userInfo).Error
	return err
}

// DeleteUserInfo 删除UserInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (userInfoService *UserInfoService) DeleteUserInfo(userInfo autocode.UserInfo) (err error) {
	err = global.GVA_DB.Delete(&userInfo).Error
	return err
}

// DeleteUserInfoByIds 批量删除UserInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (userInfoService *UserInfoService) DeleteUserInfoByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]autocode.UserInfo{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateUserInfo 更新UserInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (userInfoService *UserInfoService) UpdateUserInfo(userInfo autocode.UserInfo) (err error) {
	err = global.GVA_DB.Save(&userInfo).Error
	return err
}

// GetUserInfo 根据id获取UserInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (userInfoService *UserInfoService) GetUserInfo(id uint) (err error, userInfo autocode.UserInfo) {
	err = global.GVA_DB.Where("id = ?", id).First(&userInfo).Error
	return
}

type UserInfo1 struct {
	UserName string `json:"userName" form:"userName" gorm:"column:user_name;comment:用户名字;size:50;"`
	Ddd      *int   `json:"ddd" form:"ddd" gorm:"column:ddd;comment:活动牛奶滴数;size:10;"`
}

// GetUserInfoListAll 获取UserInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (userInfoService *UserInfoService) GetUserInfoListAll(info autoCodeReq.UserInfoSearch) (err error, list []autocode.UserInfo) {
	// 创建db
	var userInfos []autocode.UserInfo
	db := global.GVA_DB.Model(&autocode.UserInfo{}).Order("create_time desc")
	//db := global.GVA_DB
	//db.Model(&autocode.UserInfo{}).Select("users.name, emails.email").Joins("left join emails on emails.user_id = users.id").Scan(&result{})
	//err = db.Table("user_info").Select("user_info.id").Joins("left join activity_info on activity_info.user_id = user_info.id").Scan(&userInfos).Error
	//fmt.Println(userInfos)
	err = db.Find(&userInfos).Error
	return err, userInfos
}

// GetUserInfoInfoList 分页获取UserInfo记录
// Author [piexlmax](https://github.com/piexlmax)
func (userInfoService *UserInfoService) GetUserInfoInfoList(info autoCodeReq.UserInfoSearch) (err error, list interface{}, total int64) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&autocode.UserInfo{}).Order("create_time desc")
	var userInfos []autocode.UserInfo
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.EndData != nil && info.StartData != nil {
		db.Where("create_time >= ? AND create_time <= ?", time.Unix(info.StartData.Unix(), 0), time.Unix(info.EndData.Unix()+86400-1, 0))
	}
	if info.ID != 0 {
		db = db.Where("id = ?", info.ID)
	}
	if info.UserName != "" {
		db = db.Where("user_name = ?", info.UserName)
	}
	if info.OpenId != "" {
		db = db.Where("open_id = ?", info.OpenId)
	}
	if info.SkyStatus != nil {
		db = db.Where("sky_status = ?", info.SkyStatus)
	}
	if info.ShareStatus != nil {
		db = db.Where("share_status = ?", info.ShareStatus)
	}
	if info.AskStatus != nil {
		db = db.Where("ask_status = ?", info.AskStatus)
	}
	if info.MilkStatus != nil {
		db = db.Where("milk_status = ?", info.MilkStatus)
	}
	if info.SignStatus != nil {
		db = db.Where("sign_status = ?", info.SignStatus)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}
	err = db.Limit(limit).Offset(offset).Find(&userInfos).Error
	return err, userInfos, total
}
